﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ControlsLib;
using GestionePolveriLib.BL;
using GestionePolveriLib.Facades;
using BrightIdeasSoftware;

namespace GestionePolveri2
{
    public partial class AnagraficaPOS2 : AnagraficaPOS2Base
    {
        private Dictionary<int, POS> m_modifiedPOS = null;

        public AnagraficaPOS2()
        {
            InitializeComponent();
        }

        public override void InitializeForm()
        {
            base.InitializeForm();

            this.ShowCNFGMenu = true;
            this.CanModify = false;
            this.ShowOnlyCustomButton = true;
            this.splitContainer1.Panel2Collapsed = true;

            this.objectListView1.CellEditFinishing += new CellEditEventHandler(objectListView1_CellEditFinishing);
            
            this.CustomButtonClicked += new EventHandler<CustomEventArgs<POS>>(CustomButtonClickedHandler);
            
            this.AddCustomButton("btnSalva", "Salva");

            m_modifiedPOS = new Dictionary<int, POS>();

            if (this.Principal.IsInRole("Verniciatura"))
            {
                OperatoriFacade of = new OperatoriFacade();
                Operatore m_operatore = of.GetOperatore(this.Principal.Identity.Name);
                this.Filter.FilterBag.IDOperatore = m_operatore.ID;
                this.Filter.ShowOperatoriCombo(false);
                this.IsFiltering = true;

                LoadData(this.Filter.FilterBag);
            }
            else
            {
                LoadData();
            }
        }

        void objectListView1_CellEditFinishing(object sender, CellEditEventArgs e)
        {
            POS m_pos = (POS)e.RowObject;

            m_pos.POSValue = (int)e.NewValue;

            if (m_modifiedPOS != null)
            {
                if (!m_modifiedPOS.Keys.Contains(m_pos.ID))
                {
                    m_modifiedPOS.Add(m_pos.ID, m_pos);
                }
                else
                {
                    m_modifiedPOS[m_pos.ID] = m_pos;
                }
            }
        }

        void CustomButtonClickedHandler(object sender, CustomEventArgs<POS> e)
        {
            if (e.ButtonName.Equals("btnSalva", StringComparison.InvariantCultureIgnoreCase))
            {
                if ((m_modifiedPOS != null) && (m_modifiedPOS.Count > 0))
                {
                    try
                    {
                        POSTableFacade ptf = new POSTableFacade();
                        ptf.MessageOnSuccess = "POS aggiornato con successo.";
                        ptf.MessageOnFailure = "Errore durante la modifica del POS";
                        foreach (POS p in m_modifiedPOS.Values)
                        {
                            ptf.Transact(() =>
                            {
                                ptf.Modify(p);
                            });
                        }

                        m_modifiedPOS.Clear();
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
        }

        protected override void LoadData(GestionePolveriLib.BL.FilterBags.AnagraficaPOSFilterBag filter)
        {
            base.LoadData(filter);

            POSTableFacade pf = new POSTableFacade();

            pf.RunAsync(() => pf.GetPOSData(filter), (result) =>
            {
                this.objectListView1.AddDecoration(new EditingCellBorderDecoration { UseLightbox = true });
                this.objectListView1.SetObjects((List<POS>)result, true);
            });
        }

        protected override void LoadData()
        {
            POSTableFacade pf = new POSTableFacade();

            pf.RunAsync(() => pf.GetPOSData(), (result) =>
            {
                this.objectListView1.AddDecoration(new EditingCellBorderDecoration { UseLightbox = true });
                this.objectListView1.SetObjects((List<POS>)result, true);
            });
        }

        public override void OnFilterExecuted()
        {
            base.OnFilterExecuted();

            this.IsFiltering = true;

            ManageData();
        }

        public override void OnFilterReset()
        {
            base.OnFilterReset();

            this.IsFiltering = false;

            ManageData();
        }
    }

    public class AnagraficaPOS2Base : AnagraficaManagerFilteredObjectListView<POS,
                                     GestionePolveri2.Filters.AnagraficaPOSFilter,
                                     GestionePolveriLib.BL.FilterBags.AnagraficaPOSFilterBag> { }
}
